package soundsync.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import soundsync.songfs.FSElement;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * 
 * @author Akshay
 */
public class SongSelectorDialog extends javax.swing.JDialog implements TreeSelectionListener {
	
	public static URL[] selectSong(FSElement root) {
		SongSelectorDialog ss = new SongSelectorDialog(new JFrame(), true, root);
		
		//ss.setVisible(true);
		
		if (ss.selected.isEmpty() || !ss.hit_ok) {
			return new URL[] {};
		}
		else {
			URL[] urls = new URL[ss.selected.size()];
			for (int i = 0; i < ss.selected.size(); i++) {
				urls[i] = ss.selected.get(i).getURL();
			}
			return urls;
		}
	}
	
	/**
	 * Creates new form SongSelectorDialog
	 */
	public SongSelectorDialog(java.awt.Frame parent, boolean modal, FSElement root) {
		super(parent, modal);
		
		top = makeTree(root);
		initComponents();
		
		addButton.setEnabled(false);
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				select();
				hit_ok = true;
				dispose();
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		selected = new ArrayList<FSElement>();
		
		tree.addTreeSelectionListener(this);
		tree.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mousePressed(MouseEvent e) {
				int selRow = tree.getRowForLocation(e.getX(), e.getY());
				if (selRow != -1 && e.getClickCount() == 2) {
					select();
					if (!selected.isEmpty()) {
						hit_ok = true;
						dispose();
					}
				}
			}
		});
		
		jScrollPane1.setViewportView(tree);
		
		setLocationByPlatform(true);
		setVisible(true);
	}
	
	private DefaultMutableTreeNode makeNode(FSElement fse) {
		return new DefaultMutableTreeNode(fse, true);
	}
	
	private DefaultMutableTreeNode makeTree(FSElement fse) {
		DefaultMutableTreeNode n = makeNode(fse);
		if (fse.getChildren() != null) {
			for (FSElement c : fse.getChildren()) {
				n.add(makeTree(c));
			}
		}
		return n;
	}
	
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		select();
	}
	
	private void select() {
		TreePath[] paths = tree.getSelectionPaths();
		selected = new ArrayList<FSElement>();
		addButton.setEnabled(false);
		if (paths != null) {
			for (TreePath p : paths) {
				FSElement e = (FSElement)(((DefaultMutableTreeNode)p.getLastPathComponent()).getUserObject());
				if (e.getChildren() == null) {
					selected.add(e);
					addButton.setEnabled(true);
				}
			}
		}
	}
	
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		
		addButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		tree = new JTree(top);
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		
		addButton.setText("Add");
		addButton.addActionListener(new java.awt.event.ActionListener() {
			
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});
		
		cancelButton.setText("Cancel");
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});
		
		jScrollPane1.setViewportView(tree);
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1)
										.addGroup(layout.createSequentialGroup().addComponent(addButton).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(cancelButton).addGap(0, 289, Short.MAX_VALUE)))
						.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(addButton).addComponent(cancelButton)).addContainerGap()));
		
		pack();
	}// </editor-fold>//GEN-END:initComponents
	
	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_addButtonActionPerformed
	
	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_cancelButtonActionPerformed
	
	ArrayList<FSElement> selected;
	DefaultMutableTreeNode top;
	boolean hit_ok = false;
	
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton addButton;
	private javax.swing.JButton cancelButton;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTree tree;
	// End of variables declaration//GEN-END:variables
}
